package com.stylefeng.guns.modular.hhtb.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.modular.hhtb.aliyun.OSSResultModel;
import com.stylefeng.guns.modular.hhtb.model.Configs;
import com.stylefeng.guns.modular.hhtb.dao.ConfigsMapper;
import com.stylefeng.guns.modular.hhtb.service.IConfigsService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.hhtb.util.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统配置表 服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2018-08-29
 */
@Service
public class ConfigsServiceImpl extends ServiceImpl<ConfigsMapper, Configs> implements IConfigsService {
    @Autowired
    private ConfigsMapper configsMapper;
    @Autowired
    private FileUtils fileUtils;

    @Override
    public Object updateByIds(List<Configs> configs) {
        try {
            for(Configs s : configs){
                configsMapper.updateById(s);
            }
            return true;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    public List<Map<String, Object>> configsAllist(Page<Map<String, Object>> pages, Configs configs) {
        List<Map<String, Object>> list = configsMapper.configsAllist(pages,configs);
        Integer total = configsMapper.configsAllistCount(configs);
        pages.setTotal(total);
        return list;
    }


    /**
     * 图片上传阿里云
     * @return
     * @throws IOException
     */
    @Override
    public Map<String,Object> upload(HttpServletRequest req){
        Map<String,Object> result = new HashMap<String, Object>();

        MultipartHttpServletRequest mReq  =  null;
        MultipartFile file = null;
        InputStream is = null ;
        OSSResultModel ossResult = null;
        String fileName = "";
        // 原始文件名   UEDITOR创建页面元素时的alt和title属性
        String originalFileName = "";

        try {
            mReq = (MultipartHttpServletRequest)req;
            // 从config.json中取得上传文件的ID
            file = mReq.getFile("file");
            // 取得文件的原
            fileName = file.getOriginalFilename();
            originalFileName = fileName;
            if(!StringUtils.isEmpty(fileName)){
                is = file.getInputStream();
                fileName = fileUtils.reName(fileName);
                ossResult = fileUtils.saveFile(fileName, is);
            } else {
                throw new IOException("文件名为空!");
            }

            String[] strings = new String[5];
            strings[0] = ossResult.getUrl();


            result.put("errno",0);
            result.put("data",strings);
        }
        catch (Exception e) {
            result.put("errno",1);
            result.put("data","");
            System.out.println("文件 "+fileName+" 上传失败!");
        }

        return result;
    }
@Override
   public  String selectValue (Map<String,Object> map){
        return configsMapper.selectValue(map);
   }
}
